package io.github.betacatcode.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.github.betacatcode.dao.AccountService;
import io.github.betacatcode.entity.AccountDO;
import io.github.betacatcode.model.Account;
import io.github.betacatcode.repository.AccountRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Repository
@AllArgsConstructor
public class AccountRepositoryImpl implements AccountRepository {
    private final AccountService accountService;

    @Override
    public void save(Account account) {
        AccountDO accountDO = new AccountDO();
        BeanUtils.copyProperties(account, accountDO);
        accountService.saveOrUpdate(accountDO);
    }

    @Override
    public Account getAccountByAccountNumber(String accountNumber) {
        LambdaQueryWrapper<AccountDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDO::getAccountNumber, accountNumber);
        AccountDO accountDO = Objects.requireNonNull(accountService.getOne(queryWrapper));

        Account account = new Account();
        BeanUtils.copyProperties(accountDO,account);
        return account;
    }
}
